package com.sogoodsoft.web.common;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;

/**
 * 报表辅助类，传 List<Object[]>进行展示数据
 * 报表里添加变量 f0,f1,f2,f3,f5引用object[0],object[1],object[2],object[3],object[5]
 * @author xiaozu
 *
 */
public class ListJRDataSource implements JRDataSource {

	Logger logger  = LoggerFactory.getLogger(getClass());
	private List list = new ArrayList();
	private int row = 0;
	private int col = 0;
	private int index = 0;
	private int count = 0;
	
	/**
	 * @param list 数据
	 * @param col	每行几列
	 */
	public ListJRDataSource(List list,int col){
		this.list = list;
		this.col = col;
	}

	
	@Override
	public Object getFieldValue(JRField field) throws JRException {
		index = Integer.parseInt(field.getName().substring(1));
		logger.debug(index+"="+field.getName());
		Object arr = list.get(row);		
		Object[] obj = (Object[])arr;
		//logger.debug(String.valueOf(row)+"<"+list.size());
		count++;
		return String.valueOf(obj[index]);
	}

	@Override
	public boolean next() throws JRException {
		if(count>=col){
			count = 0;
			row++;
		}
		if(row>=list.size()){
			
			return false;
		}
		
		return true;
	}

}
